package com.speech.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;
import com.speech.AppConfig;
import com.speech.R;
import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;
import com.speech.net.BasicCallBack;
//import com.speech.net.HttpCommunicationManager;
import com.speech.net.constant.EnumDeviceType;
import com.speech.net.constant.EnumRegisterType;
import com.speech.net.constant.EnumReqestActionCode;
import com.speech.net.entity.MessageC2S;
import com.speech.net.entity.MessageC2SBuilder;
import com.speech.net.entity.MessageS2C;
import com.speech.net.request.UserLogin_Request;
import com.speech.net.result.UserAuth_Result;
import com.speech.utils.DeviceUtil;
import com.speech.utils.JsonProcessor;

public class FindPasswordStep2Activity extends BaseActivity implements View.OnClickListener {

    private EditText edt_pwd1;
    private EditText edt_pwd2;

    private ImageView img_pwd1;
    private ImageView img_pwd2;

    private Button btn_login;

    private boolean bPasswordVisible1 = false;
    private boolean bPasswordVisible2 = false;
    private SweetAlertDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password_step2);
        _initView();

        final String strCaptcha = getIntent().getStringExtra("strCaptcha");
        final String strPhone = getIntent().getStringExtra("strPhone");

        img_pwd1.setOnClickListener(this);
        img_pwd2.setOnClickListener(this);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edt_pwd1.getText() != null && edt_pwd2.getText() != null
                        && edt_pwd1.getText().toString().equals(edt_pwd2.getText().toString())) {

                    dialog = new SweetAlertDialog(FindPasswordStep2Activity.this, SweetAlertDialog.PROGRESS_TYPE);
                    dialog.setTitleText("Loading");
                    dialog.setCancelable(false);
                    dialog.show();

                    UserLogin_Request login_request = new UserLogin_Request();
                    login_request.deviceType = EnumDeviceType.ANDROID;
                    login_request.phone = Long.valueOf(strPhone);
                    login_request.strPassword = edt_pwd2.getText().toString();
                    login_request.strDeviceId = DeviceUtil.getDeviceID(FindPasswordStep2Activity.this);
                    login_request.registerType = EnumRegisterType.手机号;
                    login_request.strVerifyCode = strCaptcha;



                    //TODO: 接口请求 重置密码
//                    MessageC2S msg_c2s = MessageC2SBuilder.buildGeneral_MessageC2S(FindPasswordStep2Activity.this, EnumReqestActionCode.重置密码, JsonProcessor.StcGetJson(login_request));
//                    String json = JsonProcessor.StcGetJson(msg_c2s);
//
//                    HttpCommunicationManager.getInstance().doPost(json, new BasicCallBack(FindPasswordStep2Activity.this, json, new BasicCallBack.BasicCallBackListener() {
//                        @Override
//                        public void success(MessageS2C msg_s2c) {
//                            Log.d(AppConfig.LOG_TAG, "登陆成功");
//                            SharedPreferenceHelper spf_helper = new SharedPreferenceHelper(FindPasswordStep2Activity.this, PrefsConstant.FILE_USER_INFO);
//                            spf_helper.putData(PrefsConstant.KEY_USER_INFO_INT_LOGIN_STATE, 1);
//                            UserAuth_Result rst = JsonProcessor.StcGetObject(UserAuth_Result.class, msg_s2c.strData);
//                            spf_helper.putData(PrefsConstant.KEY_USER_INFO_LONG_UID, rst.uid);
//                            spf_helper.putData(PrefsConstant.KEY_USER_INFO_LONG_TOKEN, rst.token);
//
//                            dialog.dismiss();
//                            startActivity(new Intent(FindPasswordStep2Activity.this, HomeActivity.class));
//                            finish();
//                        }
//
//                        @Override
//                        public void fail(MessageS2C msg_s2c) {
//                            dialog.dismiss();
//                            new SweetAlertDialog(FindPasswordStep2Activity.this, SweetAlertDialog.ERROR_TYPE)
//                                    .setTitleText(msg_s2c.strMsgUI)
//                                    .setConfirmText("OK")
//                                    .show();
//                        }
//
//                    }));
                } else {
                    //两次输入的密码不一致
                    dialog = new SweetAlertDialog(FindPasswordStep2Activity.this,SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("错误")
                            .setContentText("两次输入不一致")
                            .setConfirmText("OK");
                            dialog.show();
                }
            }
        });

    }

    private void _initView() {
        edt_pwd1 = findViewById(R.id.edt_find_psw_step2_password);
        edt_pwd2 = findViewById(R.id.edt_find_psw_step2_password2);

        img_pwd1 = findViewById(R.id.img_find_psw_step2_eyes1);
        img_pwd2 = findViewById(R.id.img_find_psw_step2_eyes2);

        btn_login = findViewById(R.id.btn_find_psw_step2_login);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_find_psw_step2_eyes1:
                if (bPasswordVisible1) {
                    //设置密码不可见
                    img_pwd1.setImageResource(R.drawable.eyes_close);
                    edt_pwd1.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);

                } else {
                    //设置密码可见
                    img_pwd1.setImageResource(R.drawable.eyes_open);
                    edt_pwd1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

                }
                bPasswordVisible1 = !bPasswordVisible1;
                break;
            case R.id.img_find_psw_step2_eyes2:
                if (bPasswordVisible2) {
                    //设置密码不可见
                    img_pwd2.setImageResource(R.drawable.eyes_close);
                    edt_pwd2.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);

                } else {
                    //设置密码可见
                    img_pwd2.setImageResource(R.drawable.eyes_open);
                    edt_pwd2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
                bPasswordVisible2 = !bPasswordVisible2;
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null){
            dialog.dismissWithAnimation();
        }
    }
}
